<?php
class Video {
	
	public function getTime ($url) {
		if (!is_string($url) || empty($url)) return false;
		$url = str_replace("&amp;", "&", $url);
		$arr = parse_url($url);
		$arr['host'] = str_replace('www.', '', $arr['host']);
		$url = "";
	
		switch ($arr['host']) {
			case 'youtube.com':
				$id = str_replace('v=', '', $arr['query']);
				$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$id);
				if ($xml) {
					$video = self::parseVideoEntry($xml);
					$time = gmdate("i:s",  (string) $video->length);
				}
				break;
			case 'youtu.be':
				$id = str_replace('/', '', $arr['path']);
				$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/'.$id);
				if ($xml) {
					$video = self::parseVideoEntry($xml);
					$time = gmdate("i:s",  (string) $video->length);
				}
				break;
			case 'player.vimeo.com':
			case 'vimeo.com':
				$id = str_replace('/', '', $arr['path']);
				$xml = simplexml_load_file('http://vimeo.com/api/v2/video/'.$id.'.xml');
				if ($xml) {
					$time = gmdate("i:s",  (string) $xml->video->duration);
				}
				break;
			case 'video.rutube.ru':
			case 'rutube.ru':
				$id = str_replace('/', '', str_replace('/video/', '', $arr['path']));
				$xml = simplexml_load_file("http://rutube.ru/cgi-bin/xmlapi.cgi?rt_mode=movie&rt_movie_id=".$id."&utf=1");
				if ($xml) {
					$time = gmdate("i:s",  (string) $xml->duration);
				}
				break;
			default:
				$time = "";
				break;
		}
	
		return $time;
	}
	
	public function getCode ($url) {
		if (!is_string($url) || empty($url)) return false;
		$url = str_replace("&amp;", "&", $url);
		$arr = parse_url($url);
		$arr['host'] = str_replace('www.', '', $arr['host']);
		$url = "";
	
		switch ($arr['host']) {
			case 'youtube.com':
				$id = str_replace('v=', '', $arr['query']);
				$code = '<iframe width="100%" height="100%" src="http://www.youtube.com/embed/'.$id;
				$code .= '?showinfo=0" frameborder="0" allowfullscreen></iframe>';
				break;
			case 'player.vimeo.com':
			case 'vimeo.com':
				$id = str_replace('/', '', $arr['path']);
				$code = '<iframe src="http://player.vimeo.com/video/'.$id;
				$code .= '" width="100%" height="100%" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
				break;
			case 'video.rutube.ru':
			case 'rutube.ru':
				$id = str_replace('/', '', str_replace('/video/', '', $arr['path']));
				$xml = simplexml_load_file("http://rutube.ru/cgi-bin/xmlapi.cgi?rt_mode=movie&rt_movie_id=".$id."&utf=1");
				if ($xml) {
					$url = (string) $xml->embed_url;
					$code = '<iframe width="100%" height="100%" src="'.$url;
					$code .= '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen scrolling="no"></iframe>';
				}
				break;
			default:
				$code = "";
				break;
		}
		return $code;
	}
	
	public function getPreview($data){
		$url = self::getVideoUrl($data);
		$thumbUrl = self::getVideoThumbUrl($url);
		return $thumbUrl;
	}
	
	// function to parse a video <entry>
	private function parseVideoEntry($entry) {
		$obj= new stdClass;
	
		// get nodes in media: namespace for media information
		$media = $entry->children('http://search.yahoo.com/mrss/');
		$obj->title = $media->group->title;
		$obj->description = $media->group->description;
	
		// get <yt:duration> node for video length
		$yt = $media->children('http://gdata.youtube.com/schemas/2007');
		$attrs = $yt->duration->attributes();
		$obj->length = $attrs['seconds'];
	
	
		// return object to caller
		return $obj;
	}
	
	private function getVideoUrl ($data) {
		if (preg_match("/<iframe.*?src=\"([^\"]+)\".*?><\/iframe>/i", $data, $matches)) {
			$url = $matches[1];
		}
		if (preg_match("/<object.*?>.*?<param name=\"movie\" value=\"([^\"]+)\"( \/>|><\/param>).*?<\/object>/i", $data, $matches)) {
			$url = $matches[1];
		}
		return $url;
	}
	
	private function getVideoThumbUrl ($url) {
		if (!is_string($url) || empty($url)) return false;
		$url = str_replace("&amp;", "&", $url);
		$arr = parse_url($url);
		$arr['host'] = str_replace('www.', '', $arr['host']);
		$url = "";
	
		switch ($arr['host']) {
			case 'rutube.ru':
	
				if (preg_match("/\/video\/embed\/(.+)/i", $arr['path'], $matches)) {
						
					$xml = simplexml_load_file("http://rutube.ru/cgi-bin/xmlapi.cgi?rt_mode=movie&rt_movie_id=".$matches[1]."&utf=1");
						
					if ($xml) {
						$url = (string) $xml->thumbnail_url;
					}
						
				}
				break;
			case 'video.rutube.ru':
				if (preg_match("/\/(.+)/i", $arr['path'], $matches)) {
					$s[0] = substr($arr['path'], 1, 2);
					$s[1] = substr($arr['path'], 3, 2);
					$url = "http://tub.rutube.ru/thumbs/".$s[0]."/".$s[1]."/".$matches[1]."-1-1.jpg";
				}
				//$url = "http://img-1.rutube.ru/thumbs/".$link[0].$link[1]."/".$link[2].$link[3]."/".$link."-2.jpg";
				break;
			case 'youtube.com':
				if (preg_match("/\/(embed|v)\/(.+)\/?/i", $arr['path'], $matches)) {
					$url = "http://img.youtube.com/vi/".$matches[2]."/0.jpg";
				}
				break;
			case 'player.vimeo.com':
				if (preg_match("/\/video\/(.+)\/?/i", $arr['path'], $matches)) {
					$clip_id = $matches[1];
				}
				$xml = simplexml_load_file('http://vimeo.com/api/v2/video/'.$clip_id.'.xml');
				if ($xml) {
					$url = (string) $xml->video->thumbnail_medium;
				}
				break;
			case 'vimeo.com':
				parse_str($arr['query'], $query);
				$clip_id = $query['clip_id'];
				$xml = simplexml_load_file('http://vimeo.com/api/v2/video/'.$clip_id.'.xml');
				if ($xml) {
					$url = (string) $xml->video->thumbnail_medium;
				}
				break;
			default:
				$url = "";
				break;
		}
		return $url;
	}
}
?>